<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <tree-tools :tree-node="company" :is-root="true" @addDepts="addDepts" />
        <!-- 以上是最上方公司信息行 -->
        <el-tree
          :data="departs"
          :props="defaultProps"
          :default-expand-all="true"
        >
          <tree-tools
            slot-scope="{ data }"
            :tree-node="data"
            @delDepts="getDepartments"
            @addDepts="addDepts"
            @editDepts="editDepts"
          />
        </el-tree>
      </el-card>
      <add-dept
        ref="addForm"
        :show-dialog.sync="showDialog"
        :tree-node="node"
        @addDepts="getDepartments"
      />
    </div>
  </div>
</template>

<script>
import TreeTools from './components/tree-tools'
import AddDept from './components/add-dept'
import { getDepartments } from '@/api/departments'
import { tranListToTreeData } from '@/utils'

export default {
  components: {
    TreeTools,
    AddDept
  },
  data() {
    return {
      node: {},
      company: {},
      departs: [],
      showDialog: false,
      defaultProps: {
        label: 'name',
        children: 'children'
      }
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    async getDepartments() {
      const res = await getDepartments()

      this.company = {
        name: res.companyName,
        manager: '负责人',
        id: ''
      }

      this.departs = tranListToTreeData(res.depts, '')
    },
    addDepts(node) {
      this.showDialog = true
      // 除了弹窗, 还要把被点击的那个部门传出来的数据存起来
      // 作为桥梁传给弹窗
      this.node = node
    },
    async editDepts(node) {
      await this.$refs.addForm.getDepartDetail(node.id)

      this.showDialog = true
      this.node = node
      // 编辑窗口数据什么时候加载?
      // 在这个弹窗弹起的地方进行加载
      // 其实父组件是可以通过 ref 直接获取子组件
      // 并且调用子组件的函数
    }
  }
}
</script>

<style scoped>
  .tree-card {
    padding: 30px 140px;
    font-size: 14px;
  }
</style>
